const install = (Vue, options) => {
  Vue.prototype.$hub = new Vue();

  // 公用方法
  Vue.prototype.$fn = {

    /**
     * 获取验证码
     * @param {event} e 
     */
    captcha(e) {

      const target = e.currentTarget;
      target.setAttribute('disabled', 'disabled');
      
      let time = 30;
      (function loop() {
        target.innerHTML = `${time} 秒后重试`;
        if (time) {
          time--;
          setTimeout(loop, 1000);
        } else {
          target.innerHTML = '获取验证码';
          target.removeAttribute('disabled');
        }
      })();

    },

    // 返回尺寸
    turnSize(num) {
      let kb = num/1024;
      if (kb < 1024) {
        return kb.toFixed(1) + 'K';
      }

      let mb = kb/1024;
      if (mb < 1024) {
        return mb.toFixed(1) + 'M';
      }

      return (mb/1024).toFixed(2) + 'G';
    },

    // 检查文件类型
    fileTest(type) {
      if (/image/.test(type)) {
        return 'image'
      }

    },

    // 创建密码
    randomPwd(len) {
      len = len || 4;
      const $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
      const maxPos = $chars.length;
      let pwd = '';

      for (let i = 0; i < len; i++) {
        pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
      }

      return pwd;
    },

    // 创建文件地址
    createObjectURL(blob) {
      if (window.URL) {
        return window.URL.createObjectURL(blob);
      } else if (window.webkitURL) {
        return window.webkitURL.createObjectURL(blob);
      } else {
        return null;
      }
    },

    // 释放文件
    revokeObjectURL(url) {
      if (window.URL) {
        window.URL.revokeObjectURL(url);
      } else if (window.webkitURL) {
        window.webkitURL.revokeObjectURL(url);
      }
    }

  };


}

export default {
  install
}